﻿using DongFangGuoKai.WMSPlatform.Commn.ApiResultModule;
using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using DongFangGuoKai.WMSPlatform.Commn.HttpModule;
using System.Reflection;
using System;
using DongFangGuoKai.WMSPlatform.Commn;

namespace DongFangGuoKai.WCSPlatform.WebApi.ExternalSystem.WMSConmmon
{
    /// <summary>
    /// WMS的外部接口
    /// </summary>
    public class WMSHttpClient
    {
        private readonly string wmsBaseUrl;
        private readonly ICtmHttpClient httpClient;
        private readonly ILogger<WMSHttpClient> logger;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="wmsBaseUrl">basrUrl</param>
        /// <param name="httpClient"></param>
        /// <param name="logger"></param>
        public WMSHttpClient(string wmsBaseUrl, ICtmHttpClient httpClient,ILogger<WMSHttpClient> logger)
        {
            this.wmsBaseUrl = wmsBaseUrl;
            this.httpClient = httpClient;
            this.logger = logger;
        }
        /// <summary>
        /// 获取分拨窗口号
        /// </summary>
        /// <param name="boxNumber">箱子号</param>
        /// <returns></returns>
        public WMSBaseResModel<GetNumberReq> GetNumber(string boxNumber,out string logStr)
        {
            try
            {
                var url = $"{wmsBaseUrl}External/GetNumber?boxNumber={boxNumber}";
                //var url = $"{wmsBaseUrl}TakeNumber/GetNumber?boxNumber={boxNumber}";
        
                var response = httpClient.Get<WMSBaseResModel<GetNumberReq>>(url);
             
                logStr = $"获取分拨窗口号,[Url:{url}], [入参:boxNumber={boxNumber}],[出参:{response.ToJson()}]";
                return response;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 通知WMS出库完成 叫号机
        /// </summary>
        /// <param name="model">任务号</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public WMSBaseResModel<string> NoticeOutCustomer(NoticeOutCustomerReq model, out string logStr)
        {
            try
            {
                var url = $"{wmsBaseUrl}External/ConveyorLineArrvingCustomer";
                //var response = Retry.Execute(() =>
                //{
                //    return httpClient.Post<WMSBaseResModel<string>>(url, model);
                //}, TimeSpan.FromSeconds(5), 3);
                //logStr=$"通知WMS出库完成(叫号机),Url:{url} 入参:{model.ToJson()} 出参:{response.ToJson()}";
                //return response;
                var  response = httpClient.Post<WMSBaseResModel<string>>(url, model);
                logStr = $"通知WMS出库完成(叫号机),[Url:{url}],[入参:{model.ToJson()}], [出参:{response.ToJson()}]";
                return response;

            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 通知WMS出库完成 美羊羊
        /// </summary>
        /// <param name="model">任务号</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public WMSBaseResModel<string> NoticeOutBound(NoticeOutBoundReq model, out string logStr)
        {
            try
            {
                var url = $"{wmsBaseUrl}External/CompleteOutBound";
                var response = httpClient.Post<WMSBaseResModel<string>>(url, model);
                logStr =$"通知WMS出库完成(美羊羊),[Url:{url}], [入参:{model.ToJson()}], [出参:{response.ToJson()}]";
                return response;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 通知WMS入库完成
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public WMSBaseResModel<string> NoticeInBox(string model,out string logStr)
        {
            try
            {
                var url = $"{wmsBaseUrl}External/ConveyorLineFinsh";
                var response = httpClient.Post<WMSBaseResModel<string>>(url, new { PalletBarcode = model });
                logStr=$"通知WMS入库完成,[Url:{url}], [入参:{new { PalletBarcode = model }}], [出参:{response.ToJson()}]";
                return response;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
